package cms.utils.myutiil;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

public class StrUtilF {
    private static Scanner sc;

    public StrUtilF() {
    }

    public static String getNumberString(int num, int length) {
        String str = String.valueOf(num);
        if (length < str.length()) {
            return null;
        } else if (length == str.length()) {
            return str;
        } else {
            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < length - str.length(); ++i) {
                sb.append(0);
            }

            return sb.append(str).toString();
        }
    }

    public static int getValidInteger() {
        return Integer.parseInt(sc.nextLine().trim());
    }

    static {
        sc = new Scanner(System.in);
    }

    /**
     * 将List<String>拼接成js数组格式的字符串
     * @param list
     * @return
     */
    public static String jointStr2ArrayStr4Js(List<String> list) {
        StringBuilder sb = new StringBuilder("[");
        for(int i = 0; i < list.size(); ++i) {
            if (i != list.size() - 1) {
                sb.append("'").append(list.get(i)).append("', ");
            } else {
                sb.append("'").append(list.get(i)).append("'");
            }
        }
        sb.append(']');
//        return sb.toString().replaceAll("\\\\", "/");
        return sb.toString();
    }

    public static String genUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 方法描述: 将字符串转换为Double类型
     *
     * @param str
     * @Return {@link Double}
     * @throws
     * @author duanhengfei
     * @date 2021年02月23日 16:37:27
     */
    public static Double parseStr2Double(String str) {
        //标识是否全部为数字
        boolean flag = true;
        //标识第一位不是数字的索引位置，默认-1
        int firstNonNumIndex = -1;
        //开始校验
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(!isNumOrDot(ch)) {
                firstNonNumIndex = i;
                flag = false;
                break;
            }
        }
        if(flag) {
            return Double.valueOf(str);
        } else {
            return Double.valueOf(str.substring(0, firstNonNumIndex));
        }
    }

    /**
     * 方法描述: 判断该字符是否是数字或者点
     *
     * @param ch
     * @Return {@link boolean}
     * @throws
     * @author duanhengfei
     * @date 2021年02月23日 16:41:38
     */
    private static boolean isNumOrDot(char ch) {
        return ((ch >= '0' && ch <= '9') || ch == '.');
    }

    /**
     * 方法描述: totalSize 字节数（数字），sectionSize格式化后的内存大小
     * 两者比较大小
     * totalSize > sectionSize 则返回 1，相等则返回0， 小于则返回-1
     *
     * @param totalSize
     * @param sectionSize
     * @Return {@link int}
     * @throws
     * @author duanhengfei
     * @date 2021年02月23日 16:52:40
     */
    public static int compareSize(long totalSize, String sectionSize) {
        double sectionSizeByte = 0D;
        String str = sectionSize.toLowerCase().trim();
        if(str.endsWith("kb") || str.endsWith("k")) {
            sectionSizeByte = parseStr2Double(sectionSize) * 1024;
        }
        if(str.endsWith("mb") || str.endsWith("m")) {
            sectionSizeByte = parseStr2Double(sectionSize) * 1024 * 1024;
        }
        if(str.endsWith("gb") || str.endsWith("g")) {
            sectionSizeByte = parseStr2Double(sectionSize) * 1024 * 1024 * 1024;
        }
        double ceil = Math.ceil(sectionSizeByte);
        if(totalSize > ceil) {
            return 1;
        } else if(totalSize == ceil) {
            return 0;
        } else {
            return -1;
        }
    }
}

